package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping("/depts")
    public Result findAll() {
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }

    @DeleteMapping("/depts")
    public Result delete(Integer id) {
        deptService.delete(id);
        return Result.success(null);
    }

    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result find(@PathVariable Integer id) {
         Dept dept =deptService.find(id);
        return Result.success(dept);
    }
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
         deptService.update(dept);
         return Result.success();
    }
}
